#!/bin/env ruby

require 'gtk2'
require 'net/http'

NcarFoothills = "http://www.eol.ucar.edu/cgi-bin/weather.cgi?site=fl&period=5-minute&fields=tdry&fields=rh&fields=cpres0&fields=wspd&fields=wdir&fields=raina&units=english"


class Temperature
    attr_accessor :location
    
    def initialize(location)
        @location = location
    end

    
    def getTemp
        url = URI.parse(NcarFoothills)
        begin
            req = Net::HTTP::Get.new(NcarFoothills)
            res = Net::HTTP.start(url.host, url.port) {|http|
                http.request(req)
            }


        token = "<th>Temperature</th>"
        index      = res.body.index(token) + token.length
        buffer     = res.body[index+10, 100]
        pointIndex = buffer.index(".") 
        startIndex = buffer.index(">") + 1


        temp = buffer[startIndex, pointIndex+2-startIndex]

        rescue
            $stderr.print Time.now.strftime("%a %b %e %H:%M:%S %Y") \
            + " getTemp failed: " + $! + "\n"
            raise
        end

            
        return temp
        
    end
end

Gtk::init
$icon = Gtk::StatusIcon.new



def update
    boulderTemp = Temperature.new(NcarFoothills)
    delaySec    = 300
    
    while 1
        begin
            temp = boulderTemp.getTemp
            $icon.file    = "../res/" + temp.to_i.to_s + ".ico"
            $icon.tooltip = "Boulder: " + temp
            sleep(delaySec)
        rescue
            sleep(delaySec)
            retry
        end
            
    end
end


Thread.new { update }



Gtk::main

